package ch9.ex22;

import static ch9.print.Print.*;
import ch9.ex2.Sequence;

interface Selector{
	boolean end();
	Object current();
	void next();
}

class Sequence3{
	private Object[] objects;
	private int next;
	public Sequence3(int size){
		objects = new Object[size];
	}
	public void add(Object x){
		if(next < objects.length){
			objects[next++] = x;
		}
	}
	private class ReverseSelector implements Selector{
		int i = objects.length - 1;
		public boolean end() {	return i < 0;	}
		public Object current() {	return objects[i];	}
		public void next() {	if(i >=0 ) i--;	}
	}
	private class SequenceSelector implements Selector{
		private int i;
		public boolean end() {	return i == objects.length;	}
		public Object current() {	return objects[i];	}
		public void next() {	if(i < objects.length) i++;	}
	}
	public Selector selector(){
		return new SequenceSelector();
	}
	public Selector reverseSelector(){
		return new ReverseSelector();
	}
}

public class GetRSelector {

	public static void main(String[] args) {
		Sequence3 s3 = new Sequence3(10);
		for(int i=0; i < 10; i++){
			s3.add(Integer.toString(i));
		}
		
		Selector selector = s3.reverseSelector();
		while(!selector.end()){
			print(selector.current() + " ");
			selector.next();
		}
		println();
		Selector selector2 = s3.selector();
		while(!selector2.end()){
			print(selector2.current() + " ");
			selector2.next();
		}
	}
}
